//
// Created by YueChuan on 2023/3/8.
//

#include "User.h"

const QString &User::getUserName() const {
    return userName;
}

void User::setUserName(const QString &userName) {
    User::userName = userName;
}

int User::getUpFileCount() const {
    return upFileCount;
}

void User::setUpFileCount(int upFileCount) {
    User::upFileCount = upFileCount;
}

int User::getFriendCount() const {
    return friendCount;
}

void User::setFriendCount(int friendCount) {
    User::friendCount = friendCount;
}

const QList<QString> &User::getFileList() const {
    return fileList;
}


void User::setFileList(const QList<QString> &fileList) {
    User::fileList = fileList;
}

const QMap<QString, double> &User::getFileMap() const {
    return fileMap;
}


const QList<int> &User::getFriendList() const {
    return friendList;
}


void User::insertOldFriend(const int &uid) {
    this->friendList.append(uid);
    ++this->friendCount;
}
void User::addNewFriend(const int &uid) {
    this->friendList << uid;
}

void User::deleteFriend(const QString &friendName) {

}

void User::upDataNewFile(const QString fileName) {

}

void User::deleteFile(const QString fileName) {

}

int User::getUid() const {
    return uid;
}

void User::setUid(int uid) {
    User::uid = uid;
}

void User::initUserBaseInfo() {

    auto* query = new QSqlQuery();
    query->prepare("select uname from password where uid = ?");
    query->addBindValue(this->getUid());
    query->exec();
    while(query->next())
        this->setUserName(query->value(0).toString());
    // 查询好友表，以初始化用户的好友列表
    query->prepare("select * from friend where uid = ?");
    query->addBindValue(this->getUid());
    bool oko = query->exec();
    if(oko){
        while(query->next()){
            this->insertOldFriend(query->value(2).toInt());
        }
    }
    // 查询文件表，以初始化用户的文件列表,文件路径以及文件大小
    query->prepare("select * from file where uid = ?");
    query->addBindValue(this->getUid());
    oko = query->exec();
    if(oko){
        while(query->next()) {
            QString fileName = query->value(3).toString();
            QString filePath = query->value(4).toString();
            double fileSize = query->value(5).toDouble();
            fileList << fileName;
            fileMap.insert(filePath, fileSize);
        }
    }
    delete query;
}
std::ostream *operator<<(std::ostream &os, const User &user) {
    os << "uid: " << user.getUid() <<"\nuserName: " << user.getUserName().toStdString();
}


